#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int N=2e5+10;
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//
ll a,b;
void solve()
{
	cin>>a>>b;
	ll bb=b;
	if(b>=4) bb%=4;
	if(a%2==0)
	{
		if(bb==1) cout<<a-b;
		if(bb==2) cout<<a+1;
		if(bb==3) cout<<a+b+1;
		if(bb==0) cout<<a;
	}
	else
	{
		if(bb==1) cout<<a+b;
		if(bb==2) cout<<a-1;
		if(bb==3) cout<<a-1-b;
		if(bb==0) cout<<a;
		
	}
	cout<<'\n';



}

int main()
{
	close();
	int T; cin>>T;
	while (T--) solve();
	return 0;
}

